﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;

using Microsoft.Practices.Unity;

using ERPStore.Web.Extensions;
using ERPStore.Web.Routing;

namespace ERPStore.Web.Start
{
	public class RouteConfig
	{
		public static void RegisterRoutes(RouteCollection routes, Microsoft.Practices.Unity.IUnityContainer container)
		{
			RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
			RouteTable.Routes.IgnoreRoute("{handler}.ashx");
			RouteTable.Routes.IgnoreRoute("robots.txt");
			RouteTable.Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
			RouteTable.Routes.IgnoreRoute("services/{*service}");
			RouteTable.Routes.IgnoreRoute("content/{*content}");
			RouteTable.Routes.IgnoreRoute("scripts/{*scripts}");
			RouteTable.Routes.IgnoreRoute("bundle/images/{*resources}");

			var erpStoreRoutes = container.Resolve<Services.IRoutesRegistrationService>();
			erpStoreRoutes.Register();

			var namespaces = new string[] { "ERPStore.Controllers" };

			// default routes
			routes.MapERPStoreRoute(
				"Admin"
				, "admin/{action}/{id}"
				, new { controller = "Admin", action = "Index", id = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				"CatchAll"
				, "{*catchall}"
				, new { controller = "Home", action = "CatchAll" }
				, namespaces
			);

			// Do not remove this route
			routes.MapERPStoreRoute(
				"Default"
				, "{controller}/{action}/{id}"
				, new { controller = "Home", action = "Index", id = string.Empty }
				, namespaces
			);

			var eventPublisherService = container.Resolve<EventBrokR.IPublisher>();
			eventPublisherService.PublishAsync(new Events.RegisteredRoutesEvent()).Wait();

			GlobalConfiguration.Configuration.Logger.Info("routes configured");
		}

	}
}
